﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;
using AcademicPlanner.Domain.Models.Collections;
using AcademicPlanner.Presentation.Views.Shared;
using AcademicPlanner.Framework.Helpers;

namespace AcademicPlanner.Presentation.Views.Courses
{
	/// <summary>
	/// ListCoursesViewModel represents a model object for a
	/// Courses/List view.
	/// </summary>
	/// <since>1.0</since>
	public class ListCoursesViewModel
	{
		/// <summary>
		/// Gets or sets the <c>AcademicPlan</c> whose <c>Course</c>s are being listed.
		/// </summary>
		/// <since>1.0</since>
		public AcademicPlan AcademicPlan { get; set; }

		/// <summary>
		/// Gets or sets the page number of the <c>AcademicPlan</c>'s <c>Course</c>s being viewed.
		/// </summary>
		/// <since>1.0</since>
		public uint PageNumber { get; set; }
	}


	/// <summary>
	/// List represents the web page to display and manage
	/// academic courses under an academic plan.
	/// </summary>
	/// <since>1.0</since>
	public partial class List : View<ListCoursesViewModel>
	{
		/// <summary>
		/// Gets or sets the model object for the <c>CoursesControl</c> embedded in this view
		/// for listing courses.
		/// </summary>
		/// <since>1.0</since>
		protected CoursesControlModel ViewCoursesModel { get; set; }

		/// <summary>
		/// Gets or sets the <c>ModelCollectionPage</c> of <c>Course</c>s to display.
		/// </summary>
		/// <since>1.0</since>
		protected CourseCollection.ModelCollectionPage CoursesPage { get; set; }

		/// <summary>
		/// Gets or sets the number of page links to display before and after the current page.
		/// </summary>
		/// <since>1.0</since>
		protected int PaginationRadius { get; set; }


		/// <summary>
		/// Handles the page load event for this view.
		/// </summary>
		/// <remarks>
		/// This methods intializes the page title and heading
		/// as well as the model object for the embedded control.
		/// </remarks>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details about the event.</param>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			var vItemsPerPage = Convert.ToUInt32((string)ConfigHelper
										.GetPresentationSectionValue("paginationSettings", "itemsPerPage"));

			pageHeading = Tr("My Courses");
			Title = string.Format(Tr(titleFormat), pageHeading);
			pageTip = App_LocalResources.PageTips.ListCourses;

			PaginationRadius = Convert.ToInt32((string)ConfigHelper
										.GetPresentationSectionValue("paginationSettings", "pageLinksRadius"));
			CoursesPage = Model.AcademicPlan.Courses.ToPage(Model.PageNumber, vItemsPerPage);

			ViewCoursesModel = new CoursesControlModel();
			ViewCoursesModel.Id = "myCourses";
			ViewCoursesModel.Courses = CoursesPage.Items();
		}
	}
}
